package br.com.guiav6.sisguia.utils;

import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;

import br.com.guiav6.sisguia.bean.LogradouroBean;

import com.thoughtworks.xstream.XStream;
import com.thoughtworks.xstream.io.xml.Dom4JDriver;

public abstract class MapUtil {
	
	static LogradouroBean lBean;
	
	public static LogradouroBean findPathMapaByCep(String nuCep){
		String urlServico = "http://www.cepfacil.com.br/service/?filiacao=40FAB8A1-25A9-41EB-AADB-159DA7ACDFA8&cep=#cep_busca#&formato=xml";
		urlServico = urlServico.replace("#cep_busca#", nuCep);
		try {
			URL url = new URL(urlServico);
			XStream stream = new XStream(new Dom4JDriver());
			stream.processAnnotations(LogradouroBean.class);
			lBean = (LogradouroBean) stream.fromXML(url.openStream());
			for(br.com.guiav6.sisguia.enuns.Bairro bairro: br.com.guiav6.sisguia.enuns.Bairro.values()){
				if(bairro.getDescricao().equalsIgnoreCase(lBean.getBairro())){
					lBean.setIdBairro(bairro.getId());
					lBean.setBairro(bairro.getDescricao());
					lBean.setPathMapa(bairro.getPathMapa()+"414-415.png");
					break;
				}
			}
		} catch (MalformedURLException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return lBean;
	}

}